﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using Newtonsoft.Json;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.ServiceComponent.DAC.Service;
using System.Data;
using System.Reflection;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Base.Dao;

namespace T.FAS.Runtime.DataModel.Dao
{
    public class DataQueryFilterELBDao : DaoService<DataQueryFilter>
    { 
        public override string TableName => "FAS_DataQueryFilter";
        public override string IDColumnName => "ID";
        public string ForeignColumn => "DataQueryID";
        public override List<string> ExceptColumnName {get;} = new List<string> { };

        public DataQueryFilterELBDao() : base()
        {
            DBName = "TFAS";
        }

        public override DataQueryFilter ConvertToObject(Dictionary<string, PropertyInfo> columns, DataRow row)
        {
            if (columns.ContainsKey("FilterType"))
                columns.Remove("FilterType");
            columns.Remove("IsDefault");

            var data = base.ConvertToObject(columns, row);
            data.FilterType = (FilterType)Enum.Parse(typeof(FilterType), Convert.ToString(row["FilterType"]));            
            
            string isDefaultVal = row["IsDefault"]?.ToString();
            if (!string.IsNullOrEmpty(isDefaultVal))
            {
                if (int.TryParse(isDefaultVal, out int isDefaultInt))
                {
                    data.IsDefault = isDefaultInt == 0 ? false : true;
                }
                else
                    data.IsDefault = false;
            }
            else
                data.IsDefault = false;

            return data;
        }

        public override Dictionary<string, object> GetDataMapping(DataQueryFilter data)
        {
            var mapping = base.GetDataMapping(data);
            mapping.Remove("FilterType");        
            mapping.Add("FilterType", (int)data.FilterType);

            return mapping;
        }

        /// <summary>
        /// 获取元数据内容
        /// </summary>
        /// <param name="metaDataId"></param>
        /// <returns></returns>
        public List<DataQueryFilter> GetDataQueryFilters(string dqId)
        {
            var result = new List<DataQueryFilter>();
            if (string.IsNullOrEmpty(dqId))
            {
                return result;
            }
            var dt = base.GetByFilter($"{ForeignColumn}='{dqId}'");
            result = DaoUtil.ConvertToEntity<DataQueryFilter>(dt, this.ExceptColumnName) ?? result;
            return result;
        }

        /// <summary>
        /// 添加DataQueryFilter
        /// </summary>
        /// <param name="dqFilters"></param>
        public void CreateDataQueryFilter(List<DataQueryFilter> dqFilters)
        {
            try
            {
                base.Add(dqFilters);
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DQ.CreateFilterError, e.Message, e);
            }
        }

        /// <summary>
        /// 删除DataQueryFilter
        /// </summary>
        /// <param name="dqId"></param>
        public void DeleteDataQueryFilter(string dqId)
        {
            try
            {
                base.DB.ExecSqlStatement(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, this.ForeignColumn, dqId));
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DQ.DeleteFilterError, e.Message, e);
            }
        }

        /// <summary>
        /// 删除DataQuerySource
        /// </summary>
        /// <param name="dqIds"></param>
        public void BatchDeleteDataQueryFilter(List<string> dqIds)
        {
            try
            {
                List<string> sqls = new List<string>();
                foreach (string dqId in dqIds)
                {
                    sqls.Add(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, this.ForeignColumn, dqId));
                }
                base.DB.ExecSqlStatement(sqls.ToArray());
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DQ.DeleteFilterError, e.Message, e);
            }
        }
    }
}
